package san.web.m;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import san.entity.m.Supplier;
import san.service.m.SupplierService;
import com.utils.Page;
import com.web.struts2.CRUDSupportAction;

@ParentPackage("default")
@Results( { @Result(name = "reload", location = "supplier.shtml", type = "redirect") })
public class SupplierAction extends CRUDSupportAction {
	/**
	 * 自动生成SupplierAction类.
	 */
	private static final long serialVersionUID = -1839657021172943084L;
	@Autowired
	private SupplierService supplierService;
	private Page<Supplier> page = new Page<Supplier>(10);//每页10条记录
	private Supplier supplier;
	
	@Override
	protected void prepareModel() throws Exception {
		if (supplier != null && StringUtils.isNotBlank(supplier.getId())) {
			supplier = supplierService.get(supplier.getId());
		} else {
			if (StringUtils.isNotBlank(getId())) {
				supplier = supplierService.get(getId());
			} else {
				supplier = new Supplier();
			}
		}
	}
	
	@Override
	public String delete() throws Exception {
		if (StringUtils.isNotBlank(getId())) {
			supplierService.delete(getId());
		}
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		return "input";
	}

	@Override
	public String list() throws Exception {
		setPage(supplierService.getAll(page));
		return SUCCESS;
	}

	@SuppressWarnings("finally")
	@Override
	public String save() throws Exception {
		try {
			supplierService.save(supplier);
			addActionMessage("保存成功!");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			addActionError("保存失败!");
		}finally{
			return RELOAD;
		}
	}

	public Page<Supplier> getPage() {
		return page;
	}

	public void setPage(Page<Supplier> page) {
		this.page = page;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

}
